package com.jxc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.dao.TbEmployeeRoleDAO;
import com.jxc.entity.TbEmployeeRole;

/**
 * Data access object (DAO) for domain model class TbEmployeeRole.
 * 
 * @see com.jxc.dao.TbEmployeeRole
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class TbEmployeeRoleDAOImpl implements TbEmployeeRoleDAO  {
	@Resource private SessionFactory sessionFactory;
	private static final Log log = LogFactory.getLog(TbEmployeeRoleDAOImpl.class);


	public void save(TbEmployeeRole transientInstance) {
		log.debug("saving TbEmployeeRole instance");
		try {
			sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}


	public void delete(TbEmployeeRole persistentInstance) {
		log.debug("deleting TbEmployeeRole instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}


	public TbEmployeeRole findById(java.lang.Integer id) {
		log.debug("getting TbEmployeeRole instance with id: " + id);
		try {
			TbEmployeeRole instance = (TbEmployeeRole) sessionFactory.getCurrentSession().get(
					TbEmployeeRole.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}


	public List findByExample(TbEmployeeRole instance) {
		log.debug("finding TbEmployeeRole instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria(
					TbEmployeeRole.class)
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TbEmployeeRole instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TbEmployeeRole as model where model."
					+ propertyName + "= ?";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}


	public List findAll() {
		log.debug("finding all TbEmployeeRole instances");
		try {
			String queryString = "from TbEmployeeRole";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}


	public TbEmployeeRole merge(TbEmployeeRole detachedInstance) {
		log.debug("merging TbEmployeeRole instance");
		try {
			TbEmployeeRole result = (TbEmployeeRole) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}


	public void attachDirty(TbEmployeeRole instance) {
		log.debug("attaching dirty TbEmployeeRole instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}


	public void attachClean(TbEmployeeRole instance) {
		log.debug("attaching clean TbEmployeeRole instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public List findByUserId(Object userID) {
		try {
			String queryString = "from TbEmployeeRole as a where a.tbEmployee.id='"
					+ userID.toString() + "'";
			Query queryObject = sessionFactory.getCurrentSession().createQuery(
					queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
}